package com.qire.manhua.model.vo;

import com.qire.common.support.base.BaseVO;
import com.qire.manhua.model.entity.ComicBookEntity;
import com.qire.manhua.model.entity.ComicBookEntity.DirectoryInfo;
import com.qire.manhua.model.entity.ComicBookEntity.LockInfo;
import com.qire.common.model.entity.UserEntity;
import com.qire.manhua.model.enumeration.BookStatus;
import com.qire.manhua.model.enumeration.FreeType;

import java.math.BigDecimal;
import java.util.ArrayList;

public class DirectoryVO extends BaseVO {

    public LockInfoVO lockInfo;
    public ArrayList<DirectoryItemVO> chapters;
    public int teenager;            //: 1：禁止青少年观看   0:允许青少年观看

    public class LockInfoVO {
        public int      sale_state;             //: 书籍状态，-1下架，0未上架，1上架，2为已过期          上架状态
        public String   lave_chapter_tips;      //: "会员限时免费阅读此本漫画",                        限免文字描述
        public String   chapter_coin;           //: 49,                                             原价
        public String   my_chapter_coin;        //: 49                                              我的价格
        public String   vip_chapter_coin;       //: 34.3                                            会员的价格
        public int      fee_type;               //: 3,                                              漫画收费类型
        public String   restriction_time;       //: "2019-05-31 23:59"                              限免结束时间
        public int      ad_limit;               //: 0                                               剩余解锁次数

        public void fillLockInfo(ComicBookEntity comicBookEntity) {
            UserEntity.self.setCanAdUnlockedCount(ad_limit);

            BookStatus  saleState       = BookStatus.build(sale_state);
            String      introduction    = lave_chapter_tips;
            FreeType    bookFreeType    = FreeType.build(fee_type);
            String      limitFreeTime   = restriction_time;
            float       originalPrice   = new BigDecimal(chapter_coin).floatValue();
            String      vipPriceStr     = vip_chapter_coin;
            float       vipPrice        = new BigDecimal(vipPriceStr).floatValue();
            String      buyPriceStr     = my_chapter_coin;
            float       buyPrice        = new BigDecimal(buyPriceStr).floatValue();

            LockInfo lockInfo = comicBookEntity.new LockInfo(saleState, introduction, originalPrice, vipPrice, vipPriceStr, buyPrice, buyPriceStr, bookFreeType, limitFreeTime);
            comicBookEntity.loadLockInfo(lockInfo);
        }
    }

    public class DirectoryItemVO {

        public String   chapterName;        //: "第64话",
        public int      chapterId;          //: 3721,章节ID
        public int      chapter_px;         //: 64,排序
        public String   chapter_img;        //: "https://p.qiremanhua.com/uploads/default/201905/5cd155c990f56.jpg?x-oss-process=image/quality,q_90/format,webp",
        public String   chapter_date;       //: "2018-08-16",
        public int      chapter_like;       //: 786,
        public int      chapterBuyState;    //: 1,购买状态：0-不需要购买，1-未解锁，2-已解锁，不需要购买不等同于免费。
        public int      vipFree;            //: 0,是否会员限免 1 是，0 否
        public int      ad_unlock;          //: 0,是否支持广告解锁 1-支持 0-不支持
        public int      isFree;             //: 0,是否免费  1是，0否
        public String   expired_time;       //: "2020-05-05 08:54:66"   限时过期时间:不存在则为：为空字符串

        public DirectoryInfo toDirectoryInfo(ComicBookEntity comicBookEntity, boolean isLatest) {
            boolean isVipFree = vipFree == 1;
            boolean isFreeBool = isFree == 1;
            boolean canUnlockedByAd = ad_unlock == 1;
            return comicBookEntity.new DirectoryInfo(chapter_px, chapterName, chapterBuyState, chapterId, isVipFree,
                    isFreeBool, chapter_img, chapter_date, chapter_like, canUnlockedByAd, expired_time, isLatest);
        }
    }

    public void updateComicBook(ComicBookEntity comicBookEntity) {

        if(lockInfo != null) {
            lockInfo.fillLockInfo(comicBookEntity);
        }

        if(chapters != null) {
            int size = chapters.size();
            comicBookEntity.clearDirectoryList();
            for(int index=0; index < size; index++) {
                boolean isLatest = index >= (size - 1);
                DirectoryItemVO directoryVO = chapters.get(index);
                comicBookEntity.addDirectoryInfo(directoryVO.toDirectoryInfo(comicBookEntity, isLatest));
            }
        }

    }

    public boolean isTeensVisible() {
        return teenager == 0;
    }

}
